package pers.cz.service.impl;

import pers.cz.business.SystemConfigBusiness;
import pers.cz.mapper.SystemVarCfgMapper;
import pers.cz.model.*;
import pers.cz.pub.BeanHelper;
import pers.cz.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: PostGirl-panent
 * @description: SystemConfigServiceImpl
 * @author: Cheng Zhi
 * @create: 2022-01-13 21:38
 **/
@Service
@Transactional
public class SystemConfigServiceImpl implements SystemConfigService {

    @Autowired
    BeanHelper beanHelper;

    @Value("${user.password}")
    String password;

    @Override
    public List<Role> getAllRole() {

        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);

        return business.getAllRole();
    }

    @Override
    public Integer addRole(Role role) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.addRole(role);
    }

    @Override
    public Integer updateRole(Role role) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.updateRole(role);
    }

    @Override
    public Integer deleteRole(Integer roleId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.deleteRole(roleId);
    }

    @Override
    public Role getRoleById(Integer roleId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.getRoleById(roleId);
    }

    @Override
    public List<Role> getRoleByParentId(Integer parentRoleId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.getRolesByParentId(parentRoleId);
    }

    @Override
    public List<Role> getParentRole() {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.getAllParentRole();
    }

    @Override
    public List<Integer> getAllMenuId(Integer roleId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        List<Power> allPower = business.getAllMenuId(roleId);
        List<Integer> menuIds = new ArrayList<>();
        for (Power power : allPower) {
            menuIds.add(power.getMenuId());
        }
        return menuIds;
    }

    @Override
    public void addPower(Integer roleId, Integer menuId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        business.addPower(roleId, menuId);
    }

    @Override
    public void deletePower(Integer roleId, Integer menuId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        business.deletePower(roleId, menuId);
    }

    @Override
    public List<PostgirlDtl> queryAllUserDtl() {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.queryAllUserDtl();
    }

    @Override
    public List<PostgirlDtl> queryAllUserDtlByPage(Integer limit, Integer start) {

        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.queryAllUserDtlByPage(limit, start);
    }

    @Override
    public Integer addUser(Postgirl postgirl) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.addUser(postgirl);
    }

    @Override
    public Integer updateUser(Postgirl postgirl) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.updateUser(postgirl);
    }

    @Override
    public Integer deleteUser(Integer userId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.deleteUser(userId);
    }

    @Override
    public Integer resetPassword(Integer userId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.resetPassword(userId, password);
    }

    @Override
    public Integer getCount() {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.getCount();
    }

    @Override
    public Integer updateLockedState(Integer userId, Integer locked) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.updateLockedState(userId, locked);
    }

    @Override
    public Integer updateEnableState(Integer userId, Integer enable) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.updateEnableState(userId, enable);
    }

    @Override
    public Integer getCurrentRoleByUserId(Integer userId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.getCurrentRoleByUserId(userId);
    }

    @Override
    public PostgirlDtl getCurrentUserDtlByUserId(Integer userId) {
        SystemConfigBusiness business = new SystemConfigBusiness();
        business.setMapper(beanHelper);
        return business.getCurrentUserDtlByUserId(userId);
    }

    @Override
    public List<SystemVarCfg> getSystemVarCfg(String varType) {
        return beanHelper.getBean(SystemVarCfgMapper.class).selectByVarType(varType);
    }

    @Override
    public Integer updateSystemVarCfg(SystemVarCfg systemVarCfg) {
        int i = beanHelper.getBean(SystemVarCfgMapper.class).updateByPrimaryKey(systemVarCfg);
        return i;
    }

}
